package com.myidis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auxiliary.UserListForm;
import com.myidis.entity.User;
import com.myidis.request.PasswordReq;
import com.myidis.response.UserRes;
import com.myidis.servlet.UserServlet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {
	@Autowired
	private UserServlet userServlet;
	
	@ResponseBody
	@RequestMapping("/list")
	@ApiOperation("获取所有用户的信息列表")
	public List<UserRes> list(HttpServletRequest req) {
		List<UserRes> users;
		if(req.getParameter("dept") != null) {
			users = userServlet.getByDept(Integer.parseInt(req.getParameter("dept")));
		}
		else if(req.getParameter("group") != null) {
			users = userServlet.getByGroup(Integer.parseInt(req.getParameter("group")));
		}
		else
			users = userServlet.all();
		
		return users;
	}
	
	@ResponseBody
	@RequestMapping("/listForm")
	@ApiOperation("获取所有用户的可视化列表")
	public UserListForm listFrom(HttpServletRequest req) {
		return userServlet.form();
	}
	
	@ResponseBody
	@RequestMapping("/entity")
	@ApiOperation("获取一个用户的信息")
	public User one(@RequestBody User user) {
		return userServlet.getOne(user.getId());
	}
	
	@ResponseBody
	@RequestMapping("/entity/other")
	@ApiOperation("通过用户名获取一个用户的信息")
	public User oneByOther(User req) {
		if(req.getUserName() != null)
			return userServlet.getByUN(req.getUserName());
		else
			return null;
	}
	
	@ResponseBody
	@RequestMapping("/add")
	@ApiOperation("添加一个用户")
	public int add(@RequestBody User user) {
		return userServlet.insert(user);
	}
	
	@ResponseBody
	@RequestMapping("/addAll")
	@ApiOperation("获取一组用户")
	public int addAll(@RequestBody List<User> users) {
		return userServlet.insertAll(users);
	}
	
	@ResponseBody
	@RequestMapping("/upd")
	@ApiOperation("修改一个用户")
	public int upd(@RequestBody User user) {
		System.out.println(user.getName());
		return userServlet.update(user);
	}
	
	@ResponseBody
	@RequestMapping("/psw")
	@ApiOperation("修改用户密码")
	public int psw(@RequestBody PasswordReq req) {
		return userServlet.pswUpd(req);
	}

	@ResponseBody
	@RequestMapping("/del")
	@ApiOperation("删除一个用户")
	public int del(@RequestBody int id) {
		return userServlet.delete(id);
	}
	
	@ResponseBody
	@RequestMapping("/delAll")
	@ApiOperation("删除一组用户")
	public int delAll(@RequestBody int[] ids) {
		return userServlet.deleteAll(ids);
	}
}
